﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Microsoft.SharePoint;
using ServBus.ListForm2010.Utility;

namespace ServBus.ListForm2010.ApplicationPages
{
    [WebService(Namespace = "http://servbus.com/ListForm2010/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetSettings : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";
            SPList formList = SPContext.Current.Web.Lists[new Guid(context.Request.QueryString["List"])];
            SPFolder folder = formList.RootFolder;
            string result = string.Format("{{\"FormOpen\":{0}, \"Toolbar\":{1}",
                folder.Properties.ContainsKey(Constants.FORM_OPEN_KEY) ? folder.Properties[Constants.FORM_OPEN_KEY] : "1",
                folder.Properties.ContainsKey(Constants.TOOLBAR_KEY) ? folder.Properties[Constants.TOOLBAR_KEY] : "2");
            foreach (string key in context.Request.QueryString.Keys)
            {
                if (key == "List")
                    continue;
                result += string.Format(", \"{0}\":\"{1}\"",
                    key, context.Request.QueryString[key]);
            }
            result += "}";
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
